//import Vue from 'vue/dist/vue.runtime.esm'
import Vue from 'vue'
import Home from './App'

import router from "./index"
Vue.use(router)

import store from "./store"
Vue.use(store)

import ElementUi from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUi);

import mavonEditor from 'mavon-editor'
Vue.use(mavonEditor)

import cache from "./base/local_cache"
import event from "./base/event_loop"
import Listener from "./base/listen"
import cst from "./base/cst"
import TimerTask from './base/timer'
import ajax from './base/ajax'
import http from "./base/http"

Vue.prototype.$cache = cache;
Vue.prototype.$event = event;
Vue.prototype.$map = new Map();
Vue.prototype.$http = http;
//  初始化为登录中这种状态  需要token相关的页面，在创建的时候 需要判定这个状态
Vue.prototype.$listener = new Listener(cst.$login.inLogin);

//  注册组件
//import Text0 from "../../components/page/Text0"
//Vue.component("wText",Text0)
// import components0 from '../../components/page/plugin';
// Vue.use(components0)

let vue = new Vue({
    store,
    router,
    render: h => h(Home),
    created() {
        let loginUser = cache.getLoginUser();
        if(loginUser != null){
            let u0 = {
                username:loginUser.username,
                password:loginUser.password
            }
            ajax.post("/token.do?api=login",u0,cst.$ajax.login)
        }else{
            //  设置成不登陆
            this.$listener.publish(cst.$login.noLogin)
        }

    }
})
vue.$mount("#app");

Vue.prototype.$timer = new TimerTask(3000);

Vue.prototype.$show = function (msg, type) {
    this.$message({
        message: msg,
        type: type,
        showClose: true
    })
};

//  如果登录状态发生了改变 在这里进行订正  调整$listener中的状态 其他页面根据这个状态
//  这里是不需要取消注册 ，这里是最大的生命周期
event.register("main.js",{
    listen:[cst.$ajax.login],
    callback(event){
        let r = event.data.data;
        if(r.code == 0){
            //  保存登录用户
            vue.$cache.putLoginUser(r.data)
            vue.$listener.publish(cst.$login.login)
            vue.$store.state.user = r.data;
        }else{
            vue.$listener.publish(cst.$login.noLogin)
        }
    }
})

//  设置定时巡检
vue.$timer.start();
vue.$timer.register("main.js",{
    check(){
        //console.log("timer timer timer")
    }
})

//  生成唯一ID
Vue.prototype.uniqueId = 1;
Vue.prototype.$getUniqueId = function(){
    let id = this.uniqueId++;
    return "id_ksv"+id;
}


Vue.component('system_create', {
    render: function (createElement) {
        //console.log(this.node)
        return createElement(
            this.nodeName,   // 标签名称
            this.$slots.default
        )
    },
    props: {
        nodeName: {
            type: String,
            required: true
        },
        node:{
            type:Object||null
        }
    }
});